using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SoinusLibrary;
using SoinusLibrary.Log;

namespace HomeLite.View
{
    public partial class FormFolders : Form
    {
        #region EventHandlers
        public event FolderHandler AddFolder;
        public event FolderHandler DeleteFolder;
        #endregion

        #region Singleton
        private static FormFolders _instance;

        public static FormFolders Instance
        {
            get
            {
                if (_instance == null) _instance = new FormFolders();
                return _instance;
            }
        }
        #endregion

        #region Constructor
        private FormFolders()
        {
            InitializeComponent();
            this.lbFolderList.Items.AddRange(Settings.Default.HomeLite_Directories);
        }
        #endregion

        #region Methods
        public void AddFolderToList(string folder)
        {
            this.lbFolderList.Items.Add(folder);
            this.Update();
        }

        public void RemoveFolderFromList(string folder)
        {
            this.lbFolderList.Items.Remove(folder);
            this.Update();
        } 
        #endregion

        #region Events
        private void btAddFolder_Click(object sender, EventArgs e)
        {
            try
            {
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    AddFolder(this, new FolderEventArgs(folderBrowserDialog.SelectedPath));                    
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.WriteLog(new LogEntry(DateTime.Now, ex.Source, ex.Message));
            }
        }

        private void btDeleteFolder_Click(object sender, EventArgs e)
        {
            if (lbFolderList.SelectedItem != null)
            {
                DeleteFolder(this, new FolderEventArgs((string)lbFolderList.SelectedItem));
            }
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            this.Hide();            
        }

        private void FormFolders_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Hide();
        } 
        #endregion
    }

    public delegate void FolderHandler(Object sender, FolderEventArgs e);

    public class FolderEventArgs : EventArgs
    {
        private string folder;

        public string Folder
        {
            get { return folder; }
            set { folder = value; }
        }

        public FolderEventArgs(string pFolder)
        {
            Folder = pFolder;
        }
    }

    public class OkButtonClickEventArgs : EventArgs
    {
        private int foldersCount;

        public int FoldersCount
        {
            get { return foldersCount; }
            set { foldersCount = value; }
        }

        public OkButtonClickEventArgs(int pFoldersCount)
        {
            FoldersCount = pFoldersCount;
        }
    }
}